//
//  IconButton.swift
//  iPad_QQ
//
//  Created by 李善武 on 16/4/30.
//  Copyright © 2016年 李善武. All rights reserved.
//

import UIKit

class IconButton: UIButton {
  
    var isLandscape : Bool = false
    
    // 初始化图标以及标题设置
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.setImage(UIImage(named: "icon"), forState: .Normal)
        self.setTitle("奋斗", forState: .Normal)
        titleLabel?.textAlignment = .Center
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func rotateToLandscape(isLandscape : Bool){
        // 保存横屏、竖屏属性
        self.isLandscape = isLandscape
        
        self.frame.size.width = isLandscape ? kIconButtonLandscapeWidth : kIconButtonWH
        self.frame.size.height = isLandscape ? kIconButtonLandscapeHeight : kIconButtonWH
        self.frame.origin.y = kIconButtonY
        self.frame.origin.x = ((superview?.frame.width)! - self.frame.size.width)*0.5
        
    }
}


// 设置图标以及标题的位置
extension IconButton{

    // 重写图标Rect
    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
        if !isLandscape{// 竖屏
           return bounds
        }else{// 横屏
            let iconW : CGFloat = frame.width
            let iconH : CGFloat = iconW
            let iconX : CGFloat = 0
            let iconY : CGFloat = 0
            return CGRectMake(iconX, iconY, iconW, iconH)
        }
    }
    
    // 重写标题Rect
    override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
        if !isLandscape{// 竖屏
            return CGRectZero
        }else{// 横屏
            let titleW : CGFloat = frame.width
            let titleH : CGFloat = kIconButtonLandscapeTitleH
            let titleX : CGFloat = 0
            let titleY : CGFloat = frame.width
            
            return CGRectMake(titleX, titleY, titleW, titleH)
        }
    }
}